package com.javaee.shortlink.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.javaee.shortlink.admin.common.convention.result.Result;
import com.javaee.shortlink.admin.common.convention.result.Results;
import com.javaee.shortlink.admin.remote.ShortLinkRemoteService;
import com.javaee.shortlink.admin.remote.dto.req.ShortLinkBatchCreateReqDTO;
import com.javaee.shortlink.admin.remote.dto.req.ShortLinkCreateReqDTO;
import com.javaee.shortlink.admin.remote.dto.req.ShortLinkPageReqDTO;
import com.javaee.shortlink.admin.remote.dto.req.ShortLinkUpdateReqDTO;
import com.javaee.shortlink.admin.remote.dto.resp.ShortLinkBatchCreateRespDTO;
import com.javaee.shortlink.admin.remote.dto.resp.ShortLinkCreateRespDTO;
import com.javaee.shortlink.admin.remote.dto.resp.ShortLinkGroupCountQueryRespDTO;
import com.javaee.shortlink.admin.remote.dto.resp.ShortLinkPageRespDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 短链接控制层
 */
@RestController(value = "shortLinkControllerByAdmin")
@RequiredArgsConstructor
public class ShortLinkController {
    private final ShortLinkRemoteService shortLinkRemoteService;

    @PostMapping("/api/short-link/admin/v1/create")
    public Result<ShortLinkCreateRespDTO> createShortLink(@RequestBody ShortLinkCreateReqDTO requestParam){
        return shortLinkRemoteService.createShortLink(requestParam);
    }

    /**
     * 批量创建短链接
     */
    @PostMapping("/api/short-link/admin/v1/create/batch")
    public Result<ShortLinkBatchCreateRespDTO> batchCreateShortLink(@RequestBody ShortLinkBatchCreateReqDTO requestParam) {
        return shortLinkRemoteService.batchCreateShortLink(requestParam);
    }

    /**
     * 分页查询短链接
     */
    @GetMapping("/api/short-link/admin/v1/page")
    public Result<Page<ShortLinkPageRespDTO>> pageShortLink(ShortLinkPageReqDTO requestParam) {
        return shortLinkRemoteService.pageShortLink(requestParam.getGid(), requestParam.getOrderTag(), requestParam.getCurrent(), requestParam.getSize());
    }

    /**
     * 更新短链接
     */
    @PostMapping("/api/short-link/admin/v1/update")
    public Result<Void> updateShortLink(@RequestBody ShortLinkUpdateReqDTO requestParam){
        shortLinkRemoteService.updateShortLink(requestParam);
        return Results.success();
    }

    /**
     * 查询短链接分组内数量
     */
    @GetMapping("/api/short-link/admin/v1/count")
    public Result<List<ShortLinkGroupCountQueryRespDTO>> listGroupShortLinkCount(@RequestParam("requestParam") List<String> requestParam) {
        return shortLinkRemoteService.listGroupShortLinkCount(requestParam);
    }
}
